// pages/game/yizhi/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topList: [1, 2, 3, 4, 5, 6],
    botList: [1, 2, 3, 4, 5, 6],
    corlorList: {
      1: '#FF0000',
      2: '#FF7D00',
      3: '#FFFF00',
      4: '#00FF00',
      5: '#0000FF',
      6: '#00FFFF',
      7: '#FF00FF',
    },
    clickNum: 0,
    firstIndex:0,
    red:false,
    green:false,
    step:0,
    hide:true,
    clickIndex:-1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let list = this.shuffleArray(this.data.botList)
    let list_ = this.shuffleArray(this.data.topList)
    this.setData({
      botList: list,
      topList: list_
    })
  },
  shuffleArray(array) {
    for (let i = array.length - 1; i > 0; i--) {
      const j = Math.floor(Math.random() * (i + 1));
      [array[i], array[j]] = [array[j], array[i]];
    }
    return array;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  dianji(e) {
    //点击第一次时
    this.setData({
      clickNum: this.data.clickNum+=1,
    })
    if(this.data.clickNum==1){
      this.setData({
        firstIndex:e.currentTarget.dataset.index,
        clickIndex:e.currentTarget.dataset.index
      })
    }
    if(this.data.clickNum==2){
      
      if(this.data.firstIndex==e.currentTarget.dataset.index)  {
        this.setData({
          firstIndex:0,
          clickIndex:-1,
          clickNum:0
        })
        return wx.showToast({
          title: `不能跟自己交换卡bug`,
          icon: "none",
          image: "",
          duration: 2000,
          mask: false, 
        });
      }
      this.setData({
        step: this.data.step+=1,
        clickIndex:-1
      })
      //第二次点击的下标值存起来
      let list=this.data.topList
      let a=list[e.currentTarget.dataset.index]
      list[e.currentTarget.dataset.index]=list[this.data.firstIndex]
      list[this.data.firstIndex]=a

      this.setData({
        clickNum: 0,
        topList: list,
      })
      if(JSON.stringify(this.data.botList)===JSON.stringify(this.data.topList)){
        this.setData({
          hide: false,
        })
        return wx.showToast({
          title: `恭喜通过,一共用了${this.data.step}步`,
          icon: "none",
          image: "",
          duration: 2000,
          mask: false, 
        });
    
      }
      if(this.data.botList[this.data.firstIndex]==this.data.topList[this.data.firstIndex]||this.data.botList[e.currentTarget.dataset.index]==this.data.topList[e.currentTarget.dataset.index]){
        this.setData({
          green: true,
        })
      }else{
        this.setData({
          red: true,
        })
      }
      let timer=null
      clearTimeout(timer)
      timer = setTimeout(() => {
        this.setData({
          red: false,
          green: false
        })
      }, 1000);
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})